-----------------------------------------------------------------------
-- File:        Dac_Controller.adb
-- Description: Provides an interface to the hardware control of the Dac 		
--              voltage control, it allows the Dac to be set and it returns
--		    the current Dac voltage of a train
--
-- Requires:    Io_Ports,Unsigned_Types,Raildefs,Dio192defs, Dda06defs,
--              Int32defs
--
--	
-- Author:     Maria Mitrevska
-----------------------------------------------------------------------
with Io_Ports;
with Unsigned_Types;
use Unsigned_Types;  -- 1.8
with Raildefs, Dio192defs, Dda06defs, Int32defs, Halls2;
use Raildefs;

package body Dac_Controller is --PROTECTED

   --Declaration of types
   type Dac_Value_Array is array (Raildefs.Dac_Id) of Unsigned_8;

   protected Object is

      --Declaration of protected procedures

      --Set Voltage
      procedure Set_Voltage (
            D     : in     Raildefs.Dac_Id;
            Value : in     Unsigned_8);

      --Get Voltage
      procedure Get_Voltage (
            D     : in     Raildefs.Dac_Id;
            Value :    out Unsigned_8);

   private
      --Private members
      Dac_Values: Dac_Value_Array;

   end Object;


   protected body Object is

      --Set voltage
      procedure Set_Voltage (
            D     : in     Raildefs.Dac_Id;
            Value : in     Unsigned_8) is
         use         Raildefs,
         Dda06defs;

         TempLow  : Unsigned_8;
         TempHigh : Unsigned_8;
         --  use      Ada.Text_Io;
      begin

         TempLow := Unsigned_8(Value*(2**3));
         TempHigh := 2**3 + Unsigned_8(Value/(2**5));
         Io_Ports.Write_Io_Port( Dalo_Addr(D),Unsigned_8(TempLow));
         Io_Ports.Write_Io_Port( Dahi_Addr(D),Unsigned_8(TempHigh));
         Dac_Values(D) := Value;
      end Set_Voltage;

      --Get voltage
      procedure Get_Voltage (
            D     : in     Raildefs.Dac_Id;
            Value :    out Unsigned_8) is
      begin
         Value := Dac_Values(D);
      end Get_Voltage;

   end Object;

   --Public procedures

   --Set Voltage
   procedure Set_Voltage (
         D     : in     Raildefs.Dac_Id;
         Value : in     Unsigned_8) is
   begin
      Object.Set_Voltage(D,Value);
   end Set_Voltage;

   --Get voltage
   procedure Get_Voltage (
         D     : in     Raildefs.Dac_Id;
         Value :    out Unsigned_8) is

   begin
      Object.Get_Voltage(D,Value);
   end Get_Voltage;


end Dac_Controller;